<?php

class PC4_Controlador_Crud_Blog extends TICE_Controlador_Crud
{
	
    public function init() {
        $this->tabela = 'blog';
        $this->nomeSecao = 'Blog';
    	$this->view->campos = array('id', 'nome', 'Usuários' => 'usuario');
    }

    protected function getBean($id = 0) {
    	$bean = parent::getBean($id);
    	if (!empty($bean[$this->campoSenha])) {
    		$bean[$this->campoSenha] = md5($bean[$this->campoSenha]);
    	}
    	else {
    		unset($bean[$this->campoSenha]);
    	}
    	return $bean;
    }
    
    protected function criaTabela() {
    	$sql =
			"CREATE TABLE IF NOT EXISTS `" . $this->tabela . "` (
			  `id` INT NOT NULL AUTO_INCREMENT ,
			  `nome` VARCHAR(60) NULL ,
			  `email` VARCHAR(45) NULL ,
			  `usuario` VARCHAR(60) NULL ,
			  `" . $this->campoSenha . "` VARCHAR(34) NULL ,
			  `role` VARCHAR(20) NULL ,
			  `ativo` TINYINT(1) NULL DEFAULT 1 ,
			  `registro` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ,
			  PRIMARY KEY (`id`) )
			ENGINE = InnoDB";
    	$db = $this->modelo->getAdapter();
    	$db->query($sql);
    	if ($this->modelo->fetchRow() == null) {
	    	$this->modelo->insert(array(
	    		'nome' => 'PC4',
	    		'usuario' => 'pc4',
	    		'senha' => '909634296908deb670d65206990e290d',
	    		'role' => 'admin',
	    		'email' => 'manutencao@pc4.com.br'
	    	));
    	}
    	return $this->modelo->info();
    }

}

